package com.lyl.myaiagent.servertools.tools;

import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpUtil;
import com.lyl.myaiagent.servertools.tools.constants.FileConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;

/**
 * @author lyl
 * @version 1.0
 * @CreateTime: 2025-08-19
 */
@Slf4j
public class ResourceDownloadTool {

    @Tool(description = "download a resource from a given URL")
    public String downLoadResource(@ToolParam(description = "the url of the resource to download") String url,
                                   @ToolParam(description = "name of the file to save the downloaded resource") String fileName){
        log.info("开始下载资源");
        String fileDir = FileConstants.FILE_SAVE_DIR + "/download";
        String filePath = fileDir + "/" + fileName;

        try {
            // 创建目录
            FileUtil.mkdir(fileDir);
            HttpUtil.downloadFile(url,filePath);
            log.info("资源下载完毕");
            return "Resource downloaded successfully to :" + filePath;
        } catch (Exception e) {
            log.error("资源下载有误");
            return "Error downloaded resource:" + e.getMessage();
        }
    }
}
